package com.jimmyhsu.nestedscrolldemo.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.jimmyhsu.nestedscrolldemo.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * Created by jimmyhsu on 2017/1/14.
 */

public class DemoFragment extends Fragment {

    @BindView(R.id.id_recyclerview)
    RecyclerView mRecyclerView;
    private Unbinder unbinder;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_demo, container, false);
        unbinder = ButterKnife.bind(this, v);
        return v;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mRecyclerView.setAdapter(new DemoAdapter());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
    }

    class DemoAdapter extends RecyclerView.Adapter<DemoViewHolder> {

        private LayoutInflater mInflater = LayoutInflater.from(getActivity());
        @Override
        public DemoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new DemoViewHolder(mInflater.inflate(R.layout.demo_item, parent, false));
        }

        @Override
        public void onBindViewHolder(DemoViewHolder holder, int position) {
            holder.getContent().setText("item " + position);
        }

        @Override
        public int getItemCount() {
            return 30;
        }
    }

    class DemoViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.id_item_content)
        TextView content;
        DemoViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public TextView getContent() {
            return content;
        }
    }
}
